package com.api.gbhg.zengping.common.res;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: gbhg-oa
 * @author: Mr.zeng
 * @create: 2020-12-28 13:50
 **/
public class Res {
    private boolean success;
    private Integer code;
    private String msg;

    /**
     * 构造方法私有，对外只暴露指定的方法，目的是使外界无法自定义，只能统一使用规定好的格式
     */
    private Res(){

    }

    /**
     * 对外暴露返回成功静态方法
     * @return
     */
    public static Res ok(int code,String msg) {
        Res r = new Res();
        r.setSuccess(true);
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    /**
     * 失败静态方法
     * @return
     */
    public static Res error(int code,String msg) {
        Res r = new Res();
        r.setSuccess(false);
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }
    private Map<String,Object> data=new HashMap<>();


    /**
     *     return this 目的是实现链式调用
     *     Result.ok().sucess().message() 或
     *     Result.error().sucess().message()
     * @param success
     * @return
     */
     public Res success(Boolean success){
         this.setSuccess(success);
         return this;
     }

    public Res message(String message){
        this.setMsg(message);
        return this;
    }

    public Res code(Integer code){
        this.setCode(code);
        return this;
    }

    public Res data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public Res data(Map<String, Object> map){
        this.setData(map);
        return this;
    }





    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
}
